import { upload } from '@/api/upload'

const Uploader = {
  // 上传文件
  post: async (options: any) => {
    console.log(options);
    const formData = new FormData();
    formData.append('file', options.file);
    try {
      // 图片上传接口在这里写
      const res: any = await upload(formData);
      options.onEnd(res.data);
      options.onSuccess(res.data);
    } catch (e) {
      console.log('出错了', e);
    }
    return options;
  },
};

export default {
  uploadFile(options: any) {
    options.filename = options.filename || 'filename';
    options.beforeUpload =
      options.beforeUpload ||
      function() {
        return true;
      };
    options.onSuccess = options.onSuccess || function() {};
    options.onError = options.onError || function() {};
    options.onLoad = options.onLoad || function() {};
    options.onStart = options.onStart || function() {};
    options.onEnd = options.onEnd || function() {};

    if (options.beforeUpload(options)) {
      options.onStart(options);
      // 开始上传文件
      console.log('开始上传文件', options);
      Uploader.post(options);
    }
  },
  uploadFiles(options: any) {
    console.log('options', options);
  },
};
